Object Oriented Programming

Homework Assignment

Problem 1

Fill in the Line class methods to accept coordinate as a pair of tuples and return the slope and distance of the line.


In [5]:
class Line(object):
    
    def __init__(self,coor1,coor2):
        self.coor1 = coor1
        self.coor2 = coor2
    
    def distance(self):
        return ((self.coor1[0] - self.coor2[0])**2 + (self.coor1[1] - self.coor2[1])**2)**(0.5)
    
    def slope(self):
        return float(self.coor1[1] - self.coor2[1])/(self.coor1[0] - self.coor2[0])

In [6]:
# EXAMPLE OUTPUT

coordinate1 = (3,2)
coordinate2 = (8,10)

li = Line(coordinate1,coordinate2)

In [7]:
li.distance()


Out[7]:
9.433981132056603

In [8]:
li.slope()


Out[8]:
1.6

Problem 2

Fill in the class


In [20]:
class Cylinder(object):
    
    pi = 3.14
    
    def __init__(self,height=1,radius=1):
        self.height = height
        self.radius = radius
        
    def volume(self):
        return Cylinder.pi*(self.radius**2)*self.height
    
    def surface_area(self):
        return 2*Cylinder.pi*self.radius*self.height + 2*Cylinder.pi*(self.radius**2)

In [22]:
# EXAMPLE OUTPUT
c = Cylinder(2,3)

In [23]:
c.volume()


Out[23]:
56.52

In [24]:
c.surface_area()


Out[24]:
94.2

In [ ]: